* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Spending"

/***
This figure displays trends in seasonally-adjusted consumer spending in the 
Affinity Solutions data, pooling states by the date on which a state-wide order 
closed non-essential businesses.
***/

* Define ranges of early and late closings
local early_min = mdy(3, 17, 2020)
local early_max = mdy(3, 24, 2020)
local late_min = mdy(3, 30, 2020)
local late_max = mdy(4, 7, 2020)

********************************************************************************
**# 1. Clean business closures and reopening data 
********************************************************************************
project, uses("${root}/data/web/data/Policy Milestones - State.csv")
import delimited "${root}/data/web/data/Policy Milestones - State.csv", clear
isid statefips date policy_description

gen policy_date = date(date, "YMD")

* Date when stay-at-home order was first enacted
gen temp = policy_date if stayathome_first_start == 1
egen stayathome_start = mean(temp), by(statefips)
assert stayathome_start == temp if !mi(temp)
drop temp

* Date when nonessential business first closed 
gen temp = policy_date if nonessential_biz_first_closed == 1
egen nonessential_biz_closed = mean(temp), by(statefips)
assert nonessential_biz_closed == temp if !mi(temp)
drop temp

* Collapse to a state-level dataset 
collapse stayathome_start nonessential_biz_closed, by(statefips statename)
format %td stayathome_start nonessential_biz_closed

tempfile milestones 
save `milestones'

* Merge in Affinity 2020 state X day data
project, uses("${root}/data/web/data/Affinity - State - Daily.csv")
import delimited "${root}/data/web/data/Affinity - State - Daily.csv", clear
keep if year == 2020
gen date = mdy(month, day, year)
isid statefips date
merge m:1 statefips using `milestones', assert(3) nogen

* Keep relevant variables 
keep statefips statename date spend_all stayathome_start nonessential_biz_closed

* Closings (early v late v nonclosers)
* We exclude the group of ambiguous states who close between Mar 25 and Mar 29
gen closedate = min(nonessential_biz_closed, stayathome_start)
sum closedate
assert r(min) == `early_min' & r(max) == `late_max'
gen late_close 		= "early" if inrange(closedate, `early_min', `early_max')
replace late_close 	= "late" if inrange(closedate, `late_min', `late_max')
replace late_close  = "nonclosers" if mi(closedate)
assert mi(late_close) if inrange(closedate, `early_max' + 1, `late_min' - 1)

* State population
rename statefips state_fips
project, uses("${root}/data/derived/ACS 2014-2018 5-Year State/ACS 2014-2018 State.dta")
merge m:1 state_fips using "${root}/data/derived/ACS 2014-2018 5-Year State/ACS 2014-2018 State.dta", assert(3) keepusing(pop_2014_2018) nogen
rename pop_2014_2018 pop_2018

* Collapse to closure group X day dataset 
collapse (mean) spend_all [w = pop_2018], by(date late_close)
format %td date

******************************************************************************
**# 2. Graph closings - Calender Time
******************************************************************************

* Set bounds for graphs
keep if inrange(date, mdy(2, 1, 2020), mdy(4, 15, 2020))

* Gen dow
gen dow = dow(date)

* Graph
replace spend_all = spend_all * 100
gen top = 20
gen bottom = -40

twoway ///
	(connected spend_all date if late_close == "early" & dow == 0, lcolor(oi1) mcolor(oi1)) ///
	(connected spend_all date if late_close == "late" & dow == 0, lcolor(oi3) mcolor(oi3)) ///
	(connected spend_all date if late_close == "nonclosers" & dow == 0, lcolor(oi2) mcolor(oi2) msymbol(triangle) lpattern(dash)) ///
	(rarea top bottom date if inrange(date, `early_min', `early_max'), color(oi1%20) lwidth(none)) ///      early closings range
	(rarea top bottom date if inrange(date, `late_min', `late_max'), color(oi3%20) lwidth(none)), ///       late closings range
	note(" ") legend(col(3) order(1 "Early (Mar 17 - 24) Closers" 2 "Late (Mar 30 - Apr 7) Closers" 3 "Non-Closers") symxsize(medlarge)) ///
	xtitle("") ytitle("Change in Consumer Spending (%)" "relative to January 2020") ///
	ylabel(-40 "-40%" -20 "-20%" 0 "0%" 20 "+20%", nogrid) ///
	yscale(range(-40 20)) ///
	xline(`early_min', lpattern(dash) lc(oi1) noextend) ///
	xline(`late_min', lpattern(dash) lc(oi3) noextend) ///
	text(23.5 `early_min' "Early" "Closings", color(oi1) size(small)) ///
	text(23.5 `late_min' "Late" "Closings", color(oi3) size(small)) ///
	xlab(`=mdy(2, 1, 2020)' `""Feb 1" "2020""' ///
		 `=mdy(2, 15, 2020)' "Feb 15" ///
		 `=mdy(2, 29, 2020)' "Feb 29" ///
		 `=mdy(3, 14, 2020)' "Mar 14" ///
		 `=mdy(3, 28, 2020)' "Mar 28" ///
		 `=mdy(4, 11, 2020)' "Apr 11" ///
		 , labsize(small))
	
oi_graph_export "${root}/results/Spending/Consumer Spending around State-Ordered Business Closures", type(${fig_type})